package com.szgr.baoyin.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.AccessoryStoreActivity;
import com.szgr.baoyin.activity.BuyProtectActivity;
import com.szgr.baoyin.activity.FinanceActivity;
import com.szgr.baoyin.activity.MyForumActivity;
import com.szgr.baoyin.activity.NewFinanceActivity;
import com.szgr.baoyin.activity.PrivilegeActivity;
import com.szgr.baoyin.activity.ProtectActivity;
import com.szgr.baoyin.activity.ProtectRuleActivity;
import com.szgr.baoyin.activity.QuestionActivity;
import com.szgr.baoyin.activity.SearchActivity;
import com.szgr.baoyin.activity.StoreActivity;
import com.szgr.baoyin.activity.ToolStoreActivity;
import com.szgr.baoyin.adapter.AccessoriesGridAdapter;
import com.szgr.baoyin.adapter.CommonImagePagerAdapter;
import com.szgr.baoyin.adapter.MachineGridAdapter;
import com.szgr.baoyin.adapter.SpecialGridAdapter;
import com.szgr.baoyin.interfaces.HomePageInterface;
import com.szgr.baoyin.model.Datas;
import com.szgr.baoyin.model.HomePageImage;
import com.szgr.baoyin.myview.CircleIndicator;
import com.szgr.baoyin.myview.IconIndicator;
import com.szgr.baoyin.myview.NoScrollGridVIew;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.JsonParser;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class HomePageFragment extends Fragment implements View.OnClickListener,HomePageInterface {

    private CircleIndicator homeIndicator;
    private CommonImagePagerAdapter pagerAdapter;
    private ViewPager homePager;
    private ImageLoader imageLoader;
    private List<String> imageUrls;
    private List<String> specialImageUrls;
    private List<String> machineImageUrls;
    private int topHeight;
    private int bottomHeight;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    Datas datas = MyApplication.getmInstance().getDatas();
                    List<HomePageImage> adv_list = datas.getAdv_list();
                    urls.add(adv_list.get(adv_list.size() - 1));
                    urls.addAll(adv_list);
                    urls.add(adv_list.get(0));
                    pagerAdapter.notifyDataSetChanged();
                    homeIndicator.notifyDataSetChanged();
                    homeIndicator.setCurrentItem(1);
                    break;
                case Constant.REQUEST_FAIL:
                    ToastUtils.showToast("加载失败");
                    break;
            }
        }
    };
    private List<HomePageImage> urls;

    public HomePageFragment(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_home_page_0924,container,false);

        initView(ret);
        return ret;
    }

    private void initView(View ret) {
        changeUI(ret);
        urls = new ArrayList<>();

        pagerAdapter = new CommonImagePagerAdapter(getActivity(), urls,true);
        homePager = (ViewPager) ret.findViewById(R.id.home_page_pager);
        homePager.setAdapter(pagerAdapter);
        homeIndicator = (CircleIndicator) ret.findViewById(R.id.home_page_indicator);
        homeIndicator.setViewPager(homePager);

//        CommonImagePagerAdapter productAdapter = new CommonImagePagerAdapter(getActivity(), urls);
//        ViewPager productPager = (ViewPager) ret.findViewById(R.id.home_page_product_view_pager);
//        productPager.setAdapter(productAdapter);
//        IconIndicator productIndicator = (IconIndicator) ret.findViewById(R.id.home_page_product_indicator);
//        productIndicator.setViewPager(productPager);

        ret.findViewById(R.id.home_page_store).setOnClickListener(this);
        ret.findViewById(R.id.home_page_baoyin_brand).setOnClickListener(this);
        ret.findViewById(R.id.home_page_other_brand).setOnClickListener(this);
        ret.findViewById(R.id.home_page_finance).setOnClickListener(this);
        ret.findViewById(R.id.home_page_forum).setOnClickListener(this);
//        ret.findViewById(R.id.home_page_search).setOnClickListener(this);
        ret.findViewById(R.id.home_page_register).setOnClickListener(this);
        ret.findViewById(R.id.home_page_accessory).setOnClickListener(this);
        ret.findViewById(R.id.home_page_jichuang).setOnClickListener(this);
        ret.findViewById(R.id.home_page_question).setOnClickListener(this);
        ret.findViewById(R.id.home_page_niubi).setOnClickListener(this);
        ret.findViewById(R.id.home_page_rongzizulin).setOnClickListener(this);
        ret.findViewById(R.id.home_page_vip).setOnClickListener(this);

        imageUrls = new ArrayList<>();
        imageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        imageUrls.add("http://t01.pic.sogou.com/00c808820a2b47b9-b569de948d5741dd-28b2d6aa2e102ca0ba22ca3029710c27.jpg");
        imageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        imageUrls.add("http://t02.pic.sogou.com/00c808820a2b47b9-0e20aae5349628bb-a027e58cf078a5010c2149a9b07edb04.jpg");
        imageUrls.add("http://t03.pic.sogou.com/00c808820a2b47b9-9d86f2a6dca9d84b-366c19967c6c710ee105ea186ca56217.jpg");
        imageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        imageUrls.add("http://t01.pic.sogou.com/00c808820a2b47b9-b569de948d5741dd-28b2d6aa2e102ca0ba22ca3029710c27.jpg");
        imageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        imageUrls.add("http://t02.pic.sogou.com/00c808820a2b47b9-0e20aae5349628bb-a027e58cf078a5010c2149a9b07edb04.jpg");
        imageUrls.add("http://t03.pic.sogou.com/00c808820a2b47b9-9d86f2a6dca9d84b-366c19967c6c710ee105ea186ca56217.jpg");
        imageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        imageUrls.add("http://t01.pic.sogou.com/00c808820a2b47b9-b569de948d5741dd-28b2d6aa2e102ca0ba22ca3029710c27.jpg");
        imageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        imageUrls.add("http://t02.pic.sogou.com/00c808820a2b47b9-0e20aae5349628bb-a027e58cf078a5010c2149a9b07edb04.jpg");
        imageUrls.add("http://t03.pic.sogou.com/00c808820a2b47b9-9d86f2a6dca9d84b-366c19967c6c710ee105ea186ca56217.jpg");

        specialImageUrls = new ArrayList<>();
        specialImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        specialImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        specialImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");

        machineImageUrls = new ArrayList<>();
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");
        machineImageUrls.add("http://t04.pic.sogou.com/00c808820a2b47b9-7a1827e21c6bfa93-31e7255a2eff44fbac9eab97e2452f6c.jpg");

        NoScrollGridVIew specialGrid = (NoScrollGridVIew) ret.findViewById(R.id.special_grid);
//        specialGrid.setAdapter(new SpecialGridAdapter(getActivity(),specialImageUrls));
        NoScrollGridVIew accessoriesGrid = (NoScrollGridVIew) ret.findViewById(R.id.accessories_grid);
//        accessoriesGrid.setAdapter(new AccessoriesGridAdapter(getActivity(),imageUrls));
        NoScrollGridVIew machineGrid = (NoScrollGridVIew) ret.findViewById(R.id.machine_grid);
//        machineGrid.setAdapter(new MachineGridAdapter(getActivity(),machineImageUrls));

//        imageLoader = MyApplication.getmInstance().getImageLoader();
        if(MyApplication.getmInstance().getDatas() == null) {
            getData();
        }
    }

    private void getData() {
        JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, Constant.URL_SPLASH, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            System.out.println("------>" + jsonObject.toString());
                            String code = jsonObject.getString("code");
                            if("200".equals(code)){
//                                JsonParser.parseSplashJson(jsonObject);
                                mHandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
                            }else{
                                mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
                            }
                        } catch (JSONException e) {
                            mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
            }
        });
        jor.setRetryPolicy(new DefaultRetryPolicy(20 * 1000,1,1.0f));
        MyApplication.getmInstance().addToRequestQueue(jor);
    }

    private void changeUI(View ret) {
        LinearLayout parent = (LinearLayout) ret.findViewById(R.id.parent);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view1 = inflater.inflate(R.layout.layout_first, null);
        View view2 = inflater.inflate(R.layout.layout_second, null);
        View view3 = inflater.inflate(R.layout.layout_third, null);
        View view4 = inflater.inflate(R.layout.layout_fourth, null);
        View view5 = inflater.inflate(R.layout.layout_fifth,null);

//        View view1 = ret.findViewById(R.id.home_page_first);
//        View view2 = ret.findViewById(R.id.home_page_second);
//        View view3 = ret.findViewById(R.id.home_page_third);
//        View view4 = ret.findViewById(R.id.home_page_fourth);

        int screenWidth = MyTool.getScreenWidth(getActivity().getWindowManager());
        int screenHeight = MyTool.getScreenHeight(getActivity().getWindowManager());

        //获取状态栏高度
        int stateBar = MyTool.getStatusBarHeight(getActivity());

        double h = (screenHeight - topHeight - bottomHeight - stateBar) * 1.0 / 4;
        System.out.println("------frag------->" + "top:" + stateBar + ",bottom:" + bottomHeight + ",h:" + h + ",stateTop:" + h);
        view1.setLayoutParams(new FrameLayout.LayoutParams(screenWidth, (int) h - 30));
        view2.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, (int) h + 10));
        view3.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, (int) h - 20));
        view4.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, (int) h + 40));
//        ViewGroup.LayoutParams params1 = view1.getLayoutParams();
//        params1.height = (int) h;
//        view1.setLayoutParams(params1);
//        ViewGroup.LayoutParams params2 = view2.getLayoutParams();
//        params2.height = (int) h;
//        view2.setLayoutParams(params2);
//        ViewGroup.LayoutParams params3 = view3.getLayoutParams();
//        params3.height = (int) h;
//        view3.setLayoutParams(params3);
//        ViewGroup.LayoutParams params4 = view4.getLayoutParams();
//        params4.height = (int) h;
//        view4.setLayoutParams(params4);

        parent.addView(view1);
        parent.addView(view2);
        parent.addView(view3);
        parent.addView(view4);
        parent.addView(view5);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.home_page_store://商城
                intent.setClass(getActivity(), StoreActivity.class);
                break;
            case R.id.home_page_baoyin_brand://维保
                intent.setClass(getActivity(), ProtectActivity.class);
                break;
            case R.id.home_page_other_brand://维保条款
                intent.setClass(getActivity(), ProtectRuleActivity.class);
                break;
            case R.id.home_page_finance://金融
                intent.setClass(getActivity(), FinanceActivity.class);
                break;
            case R.id.home_page_forum://论坛
                intent.setClass(getActivity(), MyForumActivity.class);
                break;
            case R.id.home_page_search://搜索页
                intent.setClass(getActivity(), SearchActivity.class);
                break;
            case R.id.home_page_register://用户注册
                intent.setClass(getActivity(), BuyProtectActivity.class);
                break;
            case R.id.home_page_accessory://配件商城
                intent.setClass(getActivity(), AccessoryStoreActivity.class);
                break;
            case R.id.home_page_jichuang://机床商城
                intent.setClass(getActivity(),ToolStoreActivity.class);
                break;
            case R.id.home_page_question://我要提问
                intent.setClass(getActivity(), QuestionActivity.class);
                break;
            case R.id.home_page_niubi://技术牛人
                intent.setClass(getActivity(),MyForumActivity.class);
                break;
            case R.id.home_page_rongzizulin://融资租赁
                intent.setClass(getActivity(), NewFinanceActivity.class);
                break;
            case R.id.home_page_vip:
                intent.setClass(getActivity(),PrivilegeActivity.class);
                break;
        }
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        homeIndicator.startAutoScroll();

    }

    @Override
    public void onPause() {
        super.onPause();
        homeIndicator.stopAutoScroll();
    }

    @Override
    public void back(int width, int height) {
        topHeight = width;
        bottomHeight = height;
    }
}
